package com.aitaoxue.palx.fragment;

import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.adapter.MyCourseAdapter;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.ListBaseAdapter;
import com.aitaoxue.palx.base.NoCacheBaseListFragment;
import com.aitaoxue.palx.bean.ListEntity;
import com.aitaoxue.palx.bean.MyCourseList;
import com.aitaoxue.palx.bean.MyCourseList.MyCourse;
import com.aitaoxue.palx.ui.MyCourseActivity;
import com.aitaoxue.palx.util.GsonUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class MyCourseFragment extends NoCacheBaseListFragment implements
		OnItemLongClickListener {

	private MyCourse course;

	@Override
	public void initView(View view) {
		super.initView(view);
		mListView.setOnItemLongClickListener(this);
	}

	@Override
	protected ListBaseAdapter getListAdapter() {
		return new MyCourseAdapter();
	}

	@Override
	protected void sendRequestData(Map<String, String> map) {
		AitaoXueApi.MyCourses(mCacheHandler);
	}
	
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	protected ListEntity parseList(byte[] response) throws Exception {
		String res = new String(response);

		// List<MyCourse> list = new ArrayList<MyCourse>();
		//
		// JSONArray array = new JSONArray(res);
		// int count = array.length();
		// for (int i = 0; i < count; i++) {
		// JSONObject jsonObject = array.getJSONObject(i);
		// String obj = jsonObject.getString(String.valueOf(i));
		// JSONObject json = new JSONObject(obj);
		// MyCourse course = new MyCourse(json.getString("good_kind_name"),
		// json.getString("good_price"),
		// json.getString("good_minnum"), json.getString("good_id"));
		// list.add(course);
		// }
		//
		// MyCourseList endlist = new MyCourseList(list);

		MyCourseList list = GsonUtil.GsonToBean(res, MyCourseList.class);

		return list;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			final int position, long id) {
		final AlertDialog alert = new AlertDialog.Builder(getActivity())
				.setMessage("确定删除这个课程吗")
				.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						course = (MyCourse) mAdapter.getItem(position);
						// dialog
						((MyCourseActivity) getActivity()).showWaitDialog();
						AitaoXueApi.deleteCourses(course.getGood_id(), mHandler);
						// mAdapter.removeItem(course);
						// mAdapter.notifyDataSetChanged();
					}
				}).setNegativeButton("取消", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		alert.show();

		return false;
	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			// hide dialog
			((MyCourseActivity) getActivity()).hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");
				if (code.equals("0000")) {
					mAdapter.removeItem(course);
					mAdapter.notifyDataSetChanged();
					// showToast
					((MyCourseActivity) getActivity()).showToast("删除成功", 0, 0);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.add_course, menu);
		
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_add_course:
			FragmentTransaction tr=((MyCourseActivity)getActivity()).getManager().beginTransaction();
			tr.addToBackStack(null);
			tr.replace(R.id.container, new AddCourseFragment());
			
			tr.commit();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}









